﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PluggableModulesInterface;
using Utils;
using System.Collections;
using System.Reflection;

namespace SMSROUTER
{
    public partial class FrmRouterConfig : Form
    {
        private IPluggableModule _inModule;
        public IPluggableModule outModule;

        public FrmRouterConfig(IPluggableModule inModule)
        {
            InitializeComponent();
            _inModule = inModule;
        }

        private void FrmRouterConfig_Load(object sender, EventArgs e)
        {
            try
            {
                outModule = _inModule;
                int rowIndex = 0;
                foreach (var pi in _inModule.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public).Where(p => p.CanRead))
                {
                    if (rowIndex >= tlpModuleConfig.RowStyles.Count)
                    {
                        tlpModuleConfig.RowStyles.Add(new RowStyle(SizeType.AutoSize));
                    }
                    Control ctr = null;
                    if (pi.PropertyType == typeof(string))
                    {
                        ctr = new TextBox();
                        ctr.Name = pi.Name;
                        ctr.Text = pi.GetValue(_inModule, null).ToString();
                    }
                    else if (pi.PropertyType.IsNumericType())
                    {
                        ctr = new NumericUpDown();
                        ctr.Name = pi.Name;
                        ((NumericUpDown)ctr).Increment = 0;
                        ((NumericUpDown)ctr).Minimum = int.MinValue;
                        ((NumericUpDown)ctr).Maximum = int.MaxValue;
                        ((NumericUpDown)ctr).Controls[0].Visible = false;
                        ((NumericUpDown)ctr).Value = Convert.ToDecimal(pi.GetValue(_inModule, null));
                    }
                    else if (pi.PropertyType == typeof(bool))
                    {
                        ctr = new CheckBox();
                        ctr.Name = pi.Name;
                        ((CheckBox)ctr).Checked = Convert.ToBoolean(pi.GetValue(_inModule, null));
                    }
                    if (ctr != null)
                    {
                        Label lbl = new Label();
                        lbl.Text = pi.Name;
                        lbl.AutoSize = true;
                        lbl.Anchor = AnchorStyles.Left | AnchorStyles.Right;

                        tlpModuleConfig.Controls.Add(lbl, 0, rowIndex);
                        ctr.Enabled = (pi.GetSetMethod(false) != null) && !_inModule.Running;
                        ctr.Anchor = AnchorStyles.Left | AnchorStyles.Right;
                        tlpModuleConfig.Controls.Add(ctr, 1, rowIndex);
                    }
                    rowIndex++;
                }
                tlpModuleConfig.ColumnStyles[0].SizeType = SizeType.AutoSize;
                tlpModuleConfig.ColumnStyles[1].SizeType = SizeType.Percent;
                this.Height = 65 + (rowIndex + 1) * 29;

                btnOK.Enabled = !_inModule.Running;
                this.CenterToParent();
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null && ex.InnerException.Message.Contains("Object reference not set to an instance of an object."))
                {
                    MessageBox.Show("Can not find the config file for module '" + _inModule.Name + "'", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            foreach (Control c in tlpModuleConfig.Controls)
            {
                if (c is TextBox)
                {
                    PropertyInfo pi = outModule.GetType().GetProperty(c.Name);
                    if (pi != null && pi.CanWrite)
                    {
                        pi.SetValue(outModule, Convert.ChangeType(c.Text, pi.PropertyType), null);
                    }
                }
                else if (c is NumericUpDown)
                {
                    PropertyInfo pi = outModule.GetType().GetProperty(c.Name);
                    if (pi != null && pi.CanWrite)
                    {
                        pi.SetValue(outModule, Convert.ChangeType(((NumericUpDown)c).Value, pi.PropertyType), null);
                    }
                }
                else if(c is CheckBox)
                {
                    PropertyInfo pi = outModule.GetType().GetProperty(c.Name);
                    if (pi != null && pi.CanWrite)
                    {
                        pi.SetValue(outModule, Convert.ChangeType(((CheckBox)c).Checked, pi.PropertyType), null);
                    }
                }
            }
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }
    }
}